package com.jushu.storbox.adapter;

import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jushu.storbox.R;
import com.jushu.storbox.entities.FileFolderInfo;
import com.jushu.storbox.util.PublicTools;

/**
 * 
 * move file list adapter
 * 
 */
public class MoveAdapter extends BaseAdapter {
	/** context */
	private Context context;

	/** handler to refurbish */
	private Handler handler;

	/** layout inflater */
	private LayoutInflater mInflater;

	/** cloud drive file list */
	public List<FileFolderInfo> fileList;

	/** cloud drive service */
//	private ICloudDriveService cloudDriveService;

	/**
	 * Move file Adapter Controller function
	 * 
	 * @param context
	 *            Context
	 * @param handler
	 *            Handler refresh ui
	 * @param fileList
	 *            List<FileInformation> filelist data
	 * @param cloudDriveService
	 *            IShareDriveService cloud drive service
	 */
//	public MoveAdapter(Context context, Handler handler,
//			List<FileFolderInfo> fileList, ICloudDriveService cloudDriveService) {
//		// Auto-generated constructor stub
//		this.context = context;
//		this.fileList = fileList;
//		this.handler = handler;
////		this.cloudDriveService = cloudDriveService;
//		mInflater = (LayoutInflater) context
//				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//	}
	
	/**
	 * Move file Adapter Controller function
	 * 
	 * @param context
	 *            Context
	 * @param handler
	 *            Handler refresh ui
	 * @param fileList
	 *            List<FileInformation> filelist data
	 * @param cloudDriveService
	 *            IShareDriveService cloud drive service
	 */
	public MoveAdapter(Context context, Handler handler, List<FileFolderInfo> fileList) {
		// Auto-generated constructor stub
		this.context = context;
		this.fileList = fileList;
		this.handler = handler;
//		this.cloudDriveService = cloudDriveService;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	/**
	 * ListAdapter getCount
	 */
	@Override
	public int getCount() {

		return fileList.size();
	}

	/**
	 * ListAdapter getItem
	 * 
	 * @param position
	 *            int
	 * @return FileInformation filelist get position
	 */
	@Override
	public FileFolderInfo getItem(int position) {

		return fileList.get(position);
	}

	/**
	 * ListAdapter getItemId
	 * 
	 * @param position
	 *            int
	 * @return long filelist get position
	 */
	@Override
	public long getItemId(int position) {

		return position;
	}

	/**
	 * ListAdapter getView
	 * 
	 * @param position
	 *            int
	 * @param convertView
	 *            View
	 * @param parent
	 *            ViewGroup
	 * @return View ListAdapter item view
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		if (null == convertView) {
			convertView = mInflater.inflate(R.layout.moveitem_filelist, null);
			viewHolder = new ViewHolder();
			viewHolder.fileName = (TextView) convertView
					.findViewById(R.id.item_name_text);
			viewHolder.date = (TextView) convertView
					.findViewById(R.id.item_date_text);
			viewHolder.typeImg = (ImageView) convertView
					.findViewById(R.id.item_type_img);
			viewHolder.fileSize = (TextView) convertView
					.findViewById(R.id.item_filelist_file_size);
			viewHolder.item_img_re = (RelativeLayout) convertView
					.findViewById(R.id.item_img_re);
			viewHolder.itemInformation = getItem(position);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.itemInformation = (FileFolderInfo) getItem(position);
		if (0 == viewHolder.itemInformation.getIsFile()) {// isFile 0, is folder
			viewHolder.date.setText(PublicTools
					.longToString(viewHolder.itemInformation.getCreatedAt()));
			// visible date
			viewHolder.date.setVisibility(View.VISIBLE);
			// gone file total size
			viewHolder.fileSize.setVisibility(View.GONE);
			// set folder icon
			viewHolder.typeImg.setBackgroundResource(R.mipmap.folder_im);
			viewHolder.date.setText(PublicTools
					.longToString(viewHolder.itemInformation.getModifiedAt()));
			viewHolder.fileName.setText(viewHolder.itemInformation.getName());
		}
		final View views = convertView;
		// search file onclick
		viewHolder.item_img_re.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				if (viewHolder.itemInformation.getIsFile() == 0) {
					Message msg = new Message();
					msg.what = 1;
					msg.obj = viewHolder.itemInformation;
					handler.sendMessage(msg);
				} else {
					Toast.makeText(
							context,
							context.getString(R.string.moveadapter_select_folder),
							Toast.LENGTH_SHORT).show();
				}
			}
		});
		return convertView;
	}

	/**
	 * ViewHolder adapter data class
	 */
	public class ViewHolder {
		private TextView fileName;
		private TextView date;
		private ImageView typeImg;
		private TextView fileSize;
		private RelativeLayout item_img_re;
		private FileFolderInfo itemInformation;

	}

}
